package com.miyu.common.utils.page;


import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.miyu.common.pojo.book.BookPage;
import com.miyu.common.utils.commonUtils.Comcache;
import com.miyu.common.utils.magic.GetBeanUtil;
import com.miyu.common.utils.redis.redisUtils.RedisCache;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

import javax.annotation.Resource;

/**
 * 保存缓存数据
 * @author miyu
 * @date 2022/07/12
 */
public class SaveCacheData implements Pipeline {


    static RedisCache rdc = GetBeanUtil.getBeanByClass(RedisCache.class);
    /**
     * 过程
     * @param resultItems 结果项目
     * @param task        任务
     */
    @Override
    public void process(ResultItems resultItems, Task task) {
        BookPage bookPage = resultItems.get("rule");
        String key = bookPage.getKey();
        Object data = resultItems.get("data");
        if(data!=null){
            rdc.set(key,data,bookPage.getTimeout());
        }
//        if("1".equals(type)){
//            cache.set(key,data,1800);
//            if(StringUtils.isNotBlank(data.toString())){
//                JSONArray d =  JSONArray.parseArray(data.toString());
//                Comcache.setJSONArrayByKey("bookEhcache",key,d);
//            }
//        }else {
//            /*传入JSONObject数据*/
//            if(StringUtils.isNotBlank(data.toString())){
//                JSONObject d = JSONObject.parseObject(data.toString());
//                Comcache.setJSONObjectByKey("bookEhcache",key,d);
//            }
//        }
    }
}
